import { addResizeListener, removeResizeListener, debounce } from '@/utils/util'

let debResize = debounce((el, camera, renderer) => resize(el, camera, renderer), 200)

function resize(el, camera, renderer) {
  camera.aspect = el.clientWidth / el.clientHeight // 更新摄像头
  camera.updateProjectionMatrix() // 更新摄像机的投影矩阵
  renderer.setSize(el.clientWidth, el.clientHeight) // 更新渲染器
  renderer.setPixelRatio(window.devicePixelRatio) // 更新渲染器像素比
}

// 增加resize监听
export function listenResize(el, camera, renderer) {
  // 如果需要自适应
  addResizeListener(el, () => debResize(el, camera, renderer))
}

// 销毁resize监听
export function destroyResize(el, camera, renderer) {
  el.removeChild(el.children[0])
  removeResizeListener(el, () => debResize(el, camera, renderer))
}
